/*
 * @FilePath: /web-vue/src/router/index.js
 * @Desc: 路由表
 */
import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store";
import NProgress from "nprogress"; // 引入 nprogress
import "nprogress/nprogress.css"; // 引入 nprogress 样式（必须）

// nprogress 配置
NProgress.configure({
  easing: "ease",
  speed: 500,
  showSpinner: true,
});

// 捕获路由重复抛出的异常
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

Vue.use(VueRouter);

// 路由表
const routes = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/Login.vue"),
  },
  // 后台管理布局
  {
    path: "/admin",
    name: "adminIndex",
    component: () => import("@/components/Layout/Admin/Layout.vue"),
    children: [
      //【系统日志】管理模块
      {
        path: "/view/SysLog/manage",
        name: "sysLog",
        component: () => import("@/views/SysLog"),
      },
      //【部门】管理模块
      {
        path: "/view/SysDepartment/manage",
        name: "sysDepartment",
        component: () => import("@/views/SysDepartment"),
      },
      //【菜单】管理模块
      {
        path: "/view/SysMenu/manage",
        name: "sysMenu",
        component: () => import("@/views/SysMenu"),
      },
      //【角色】管理模块
      {
        path: "/view/SysRole/manage",
        name: "sysRole",
        component: () => import("@/views/SysRole"),
      },
      //【角色菜单关联表】管理模块
      {
        path: "/view/SysRoleMenu/manage",
        name: "sysRoleMenu",
        component: () => import("@/views/SysRoleMenu"),
      },
      //【系统用户】管理模块
      {
        path: "/view/SysUser/manage",
        name: "sysUser",
        component: () => import("@/views/SysUser"),
      },
      //【用户部门关联表】管理模块
      {
        path: "/view/SysUserDept/manage",
        name: "sysUserDept",
        component: () => import("@/views/SysUserDept"),
      },
      //【用户角色关联表】管理模块
      {
        path: "/view/SysUserRole/manage",
        name: "sysUserRole",
        component: () => import("@/views/SysUserRole"),
      },
      // 个人信息模块
      {
        path: "/system/user/own_center",
        name: "individualSetting",
        component: () =>
          import("@/views/SysOwnCenter/setting/IndividualSetting"),
      },
      //【首页】
      {
        path: "/view/Index",
        name: "Index",
        component: () => import("@/views/Index"),
      },
      // -------------------------- 业务模块 ------------------------
      //【房源】管理模块
      {
        path: "/view/House/manage",
        name: "house",
        component: () => import("@/views/House"),
      },
      //【房源图片】管理模块
      {
        path: "/view/HouseImage/manage",
        name: "houseImage",
        component: () => import("@/views/HouseImage"),
      },
      //【房源类型】管理模块
      {
        path: "/view/HouseType/manage",
        name: "houseType",
        component: () => import("@/views/HouseType"),
      },
      //【租房订单】管理模块
      {
        path: "/view/HousingOrder/manage",
        name: "housingOrder",
        component: () => import("@/views/HousingOrder"),
      },
      //【首页轮播】管理模块
      {
        path: "/view/IndexCarousel/manage",
        name: "indexCarousel",
        component: () => import("@/views/IndexCarousel"),
      },
      //【餐饮】管理模块
      {
        path: "/view/Meals/manage",
        name: "meals",
        component: () => import("@/views/Meals"),
      },
      //【地区】管理模块
      {
        path: "/view/Region/manage",
        name: "region",
        component: () => import("@/views/Region"),
      },
      //【旅游景点】管理模块
      {
        path: "/view/ScenerySpot/manage",
        name: "scenerySpot",
        component: () => import("@/views/ScenerySpot"),
      },
      // //【系统参数】管理模块
      // {
      //   path: "/view/SysConfig/manage",
      //   name: "sysConfig",
      //   component: () => import("@/views/SysConfig"),
      // },
    ],
  },
   // 前台展示布局
   {
    path: "/",
    name: "frontLayout",
    component: () => import("@/components/Layout/Frontend/LayoutHorizontal"),
    children: [
           // 门户首页
           {
            path: "/index",
            name: "frontendIndex",
            component: () => import("@/pages/Index/index"),
          },
          // 景点咨询
          {
            path: "/Scenery",
            name: "sceneryIndex",
            component: () => import("@/pages/Scenery/index"),
          },
          // 景点详情页
          {
            path: "/Scenery/detail",
            name: "sceneryDetail",
            component: () => import("@/pages/Scenery/detail"),
          },
          // 住宿服务
          {
            path: "/Housing",
            name: "housingIndex",
            component: () => import("@/pages/Housing/index"),
          },
          // 房间详情页
          {
            path: "/Housing/detail",
            name: "housingDetail",
            component: () => import("@/pages/Housing/detail"),
          },
          // 订单中心
          {
            path: "/Order",
            name: "order+Index",
            component: () => import("@/pages/Order/index"),
          },
          // 个人中心
          {
            path: "/page/individual/center",
            name: "IndividualCenter",
            component: () =>
              import("@/views/SysOwnCenter/setting/IndividualSetting"),
          },
  
  ],
    
  },
  // 前台展示布局
 {
    path: "/front",
    name: "frontLayout2",
    component: () => import("@/components/Layout/Frontend/LayoutHorizontal2"),
    children: [
    
    ],
  },
  {
    path: '/register',
    component: () => import('@/views/SysUser/registerForm'),
    hidden: true
  },
];

// 排除路由拦截列表
const excluedRoute = ["/front","/register"];

const router = new VueRouter({
  // mode: "hash",
  mode: "history",
  routes,
});

// 路由拦截（before）
router.beforeEach((to, from, next) => {
  // // 开启顶部加载条
  // NProgress.start();
  // // 不存在的路由，跳回上一个正确的路由
  // if (to.matched.length == 0) {
  //   next(from.path);
  //   // 关闭顶部加载条
  //   NProgress.done();
  //   return;
  // }

  // 会话信息存在
  if (store.state.userSession) {
    next();
  } else {
    // 排除路由拦截列表
    if (excluedRoute.indexOf(to.path) !== -1) {
      next();
      return;
    }
    // 跳转到登录页
    next("/front");
  }
});

// 路由拦截（after）
router.afterEach((to, from) => {
  // 关闭顶部加载条
  NProgress.done();
});

export default router;
